package service

import (
	"FlowMaster/model"
	"bytes"
	"encoding/json"
	"io"
	"net/http"
	"net/url"
)

// 图创接口调用
func CustCountMakeRequest(urlP string, key string, cameraid string) (model.CustCountResponse, error) {

	formData := url.Values{
		"key":      {key},
		"cameraid": {cameraid}, // 将cameraid转换为字符串
	}

	req, err := http.NewRequest("POST", urlP, bytes.NewBufferString(formData.Encode()))
	if err != nil {
		return model.CustCountResponse{}, err // 返回空的ResponseData和一个错误
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	// 发送HTTP请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return model.CustCountResponse{}, err // 返回空的ResponseData和一个错误
	}
	defer resp.Body.Close()

	// 读取响应的内容
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return model.CustCountResponse{}, err // 返回空的ResponseData和一个错误
	}

	// 将JSON响应解析到结构体中
	var data model.CustCountResponse
	err = json.Unmarshal(body, &data)
	if err != nil {
		return model.CustCountResponse{}, err // 返回空的ResponseData和一个错误
	}

	return data, nil // 返回解析后的数据和nil错误
}
